package ${packageOutPath}.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cykj.common.cymall_utils.m.MConstants;
import com.cykj.common.api.users.vo.Users;

import ${packageOutPath}.vo.${bean}Vo;
import ${packageOutPath}.service.I${bean}Service;
import ${packageOutPath}.bean.${bean};
import ${packageOutPath}.api.Murl;


@Controller
public class M${bean}Controller {
	
	@Autowired
	private I${bean}Service service ;
	@Autowired
	private M${bean}Service mService;
	
	
	
	/**
	 * 获取列表分页
	 * @param bean
	 * @return
	 * @ChangeLog
	 *
	 */
	@RequestMapping(Murl.mreq_${lowercaseBean}_list)
	@ResponseBody
	public Map<String, Object> list(Integer pageNo,Integer pageSize,String oid,Integer areaId,Integer reqType, HttpServletRequest request) {
		Map<String,Object> res = new HashMap<String, Object>() ;
		String code = MConstants.SUCCESS;
		String msg = MConstants.SUCCESS_TXT ;
		
		try {
			//判断是否传递OID
			if (StringUtils.isNotEmpty(bean.getOid())) {
				Users user = new Users();
				user.setOid(bean.getOid());
				if(pageSize==null||pageSize<0){
					pageSize = 10;
				}
				PageRequest pageRequest=new PageRequest(pageNo-1, pageSize);//当前页和每页显示的条数
				
				${bean}Vo vo=new ${bean}Vo();
				vo.setOid(oid);
				vo.setIsShow(1);
				
				PageHelper<${bean}Vo> page=service.queryPageM(pageRequest, vo);
				
				res.put("list", mService.formatList(pager.getList()));
			}else{
				code = MConstants.FAIL_NOOID;
				msg = MConstants.FAIL_NOOID_TXT ;
			}
		} catch (Exception e) {
			code = MConstants.FAIL ;
			msg = e.getLocalizedMessage();
			log.error(msg) ;
			e.printStackTrace() ;
		}
		
		res.put("code", code);
		res.put("msg", msg);
		
		return res;
	} 
	
	/**
	 * 保存
	 * @param bean
	 * @return
	 * @ChangeLog
	 *
	 */
	@RequestMapping(Murl.mreq_${lowercaseBean}_save)
	@ResponseBody
	public Map<String, Object> save(${bean}Vo vo, HttpServletRequest request) {
		Map<String,Object> res = new HashMap<String, Object>();
		String code = MConstants.SUCCESS;
		String msg = MConstants.SUCCESS_TXT ;
		
		try {
			//判断是否传递OID
			if (StringUtils.isNotEmpty(bean.getOid())) {
				Users user = new Users();
				user.setOid(bean.getOid());
				res = service.save(vo) ;
				log.info("res:"+res) ;
			}else{
				code = MConstants.FAIL_NOOID;
				msg = MConstants.FAIL_NOOID_TXT ;
			}
		} catch (Exception e) {
			code = MConstants.FAIL ;
			msg = e.getLocalizedMessage();
			log.error(msg) ;
			e.printStackTrace() ;
		}
		
		res.put("code", code);
		res.put("msg", msg);
		return res;
	} 
	
	/**
	 * 更新
	 * @param bean
	 * @return
	 * @ChangeLog
	 *
	 */
	@RequestMapping(Murl.mreq_${lowercaseBean}_update)
	@ResponseBody
	public Map<String, Object> update(${bean} bean, HttpServletRequest request) {
		Map<String,Object> res = new HashMap<String, Object>();
		String code = MConstants.SUCCESS;
		String msg = MConstants.SUCCESS_TXT ;
		
		try {
			//判断是否传递OID
			if (StringUtils.isNotEmpty(bean.getOid())) {
				res = service.update(bean) ;
				log.info("res:"+res) ;
			}else{
				code = MConstants.FAIL_NOOID;
				msg = MConstants.FAIL_NOOID_TXT ;
			}
			
		} catch (Exception e) {
			code = MConstants.FAIL ;
			msg = e.getLocalizedMessage();
			log.error(msg) ;
			e.printStackTrace() ;
		}
		
		res.put("code", code);
		res.put("msg", msg);
		return res;
	} 
	
	/**
	 * 获得详情
	 * @param bean
	 * @return
	 * @ChangeLog
	 *
	 */
	@RequestMapping(Murl.mreq_${lowercaseBean}_getbyid)
	@ResponseBody
	public Map<String, Object> getById(String oid, Integer id, HttpServletRequest request) {
		Map<String,Object> res = new HashMap<String, Object>();
		String code = MConstants.SUCCESS;
		String msg = MConstants.SUCCESS_TXT ;
		try {
			//判断是否传递OID
			if (StringUtils.isNotEmpty(oid)) {
				${bean}Vo vo = service.getById(id);
				res.put("bean", mService.formatBean(vo));
				log.info("res:"+res) ;
			}else{
				code = MConstants.FAIL_NOOID;
				msg = MConstants.FAIL_NOOID_TXT ;
			}
		} catch (Exception e) {
			code = MConstants.FAIL ;
			msg = e.getLocalizedMessage();
			log.error(msg) ;
			e.printStackTrace() ;
		}
		
		res.put("code", code);
		res.put("msg", msg);
		return res;
	} 
	
	/**
	 * 根据条件查询
	 * @param bean
	 * @return
	 * @ChangeLog
	 *
	 */
	@RequestMapping(Murl.mreq_${lowercaseBean}_getbycondition)
	@ResponseBody
	public Map<String, Object> getByCondition(${bean} bean, HttpServletRequest request) {
		Map<String,Object> res = new HashMap<String, Object>();
		String code = MConstants.SUCCESS;
		String msg = MConstants.SUCCESS_TXT ;
		
		try {
			//判断是否传递OID
			if (StringUtils.isNotEmpty(bean.getOid())) {
			
				Users user = new Users();
				user.setOid(vo.getOid());
				
				${bean}Vo vo = service.getByCondition(bean,user);
				res.put("bean", mService.formatBean(vo));
				log.info("res:"+res) ;
			}else{
				code = MConstants.FAIL_NOOID;
				msg = MConstants.FAIL_NOOID_TXT ;
			}
			
		} catch (Exception e) {
			code = MConstants.FAIL ;
			msg = e.getLocalizedMessage();
			log.error(msg) ;
			e.printStackTrace() ;
		}
		
		res.put("code", code);
		res.put("msg", msg);
		return res;
	} 
	
	
	/**
	 * 删除
	 * @param id
	 * @return
	 * @ChangeLog
	 *
	 */
	@RequestMapping(Murl.mreq_${lowercaseBean}_delete)
	@ResponseBody
	public Map<String, Object> delete(String oid,Integer id, HttpServletRequest request) {
		Map<String,Object> res = new HashMap<String, Object>();
		String code = MConstants.SUCCESS;
		String msg = MConstants.SUCCESS_TXT ;
		
		try {
			//判断是否传递OID
			if (StringUtils.isNotEmpty(oid)) {
				res = service.deleteById(id);
			}else{
				code = MConstants.FAIL_NOOID;
				msg = MConstants.FAIL_NOOID_TXT ;
			}
		} catch (Exception e) {
			code = MConstants.FAIL ;
			msg = e.getLocalizedMessage();
			log.error(msg) ;
			e.printStackTrace() ;
		}
		
		res.put("code", code);
		res.put("msg", msg);
		return res;
	} 
	
	private Log log = LogFactory.getLog(M${bean}Controller.class) ;

}
